<template>
  <view style="padding-bottom: 120rpx;">
    <u-navbar
      v-if="!$isWeixin()"
      :title="title"
      :titleBold="true"
      :background="{ backgroundColor: '#ffffff' }"
      :is-fixed="true"
    ></u-navbar>

    <u-tabs active-color="#FF7A23" :list="tabList" :current="tabIndex" @change="index => (tabIndex = index)"></u-tabs>

    <view class="" v-show="tabIndex == 0">
      <design-information-form
        :showDesignType="form.ApplyObjectType == 3 || form.ApplyObjectType == 4"
        :isApply="true"
        ref="designForm"
        :showAudit="false"
      ></design-information-form>

      <u-gap :height="20" bg-color="#f5f5f5"></u-gap>
      <apply-form ref="applyForm"></apply-form>

      <block v-if="showReview">
        <u-gap height="40" bg-color="#f5f5f5"></u-gap>
        <review-form
          ref="reviewForm"
          :showReturnAudit="form.ApplyObjectType == 5 && Fill_In_Permissions > 0"
        ></review-form>
      </block>
    </view>

    <view v-if="tabIndex == 1">
      <u-cell-item
        v-if="state.ShowLevel != 1"
        :border-bottom="state.ShowLevel != 2"
        title="一级经销商名称"
        :arrow="false"
        :value="state.First_Dealer_BaseInfo.Name"
      ></u-cell-item>

      <u-cell-item
        v-if="state.ShowLevel == 3"
        :border-bottom="state.ShowLevel != 3"
        title="二级经销商名称"
        :arrow="false"
        :value="state.Double_Dealer_BaseInfo.Name"
      ></u-cell-item>

      <basic-information
        v-if="state.ShowLevel == 1 || state.ShowLevel == 2"
        :level="state.ShowLevel"
        :showOtherInfo="false"
        :showReview="false"
        :showParent="false"
        :showDeliveryAddress="false"
        :showKeDealerNo="false"
        :data="{ Dealer_BaseInfo: DealerInfo.Dealer_BaseInfo }"
        :loadCity="loadCity"
      ></basic-information>

      <store-information
        v-if="state.ShowLevel == 3"
        :loadCity="loadCity"
        :data="state.Door_Dealer_BaseInfo"
      ></store-information>
    </view>

    <view class="btn-group-fixed"><u-button v-if="showReview" type="warning" @click="submit">审核提交</u-button></view>
    <u-toast ref="uToast" />
  </view>
</template>

<script>
export default {
  data() {
    return {
      title: '门头设计审核',
      tabList: [{ name: '设计信息' }, { name: '基本信息' }],
      tabIndex: 0,
      loadCity: false,

      SelectType: 1,

      showReview: false,

      state: {
        ShowLevel: 0,
        First_Dealer_BaseInfo: {},
        Double_Dealer_BaseInfo: {},
        Door_Dealer_BaseInfo: {},
        This_Dealer_OtherInfo: {},
        This_Dealer_RelationList: [],
        DealerAuditInfo: {},
        InitialPhotoList: []
      },

      form: {
        ApplyId: '',
        DealerId: '',
        ApplyObjectType: 1,
        Remark: '',
        ApplicationRemake: '',
        ApplicationDate: null,
        DoorPlane: 0,
        OutDoorAdvertising: 0,
        Media: 0,
        InitialPhotoList: [],
        State: null,
        AuditRemark: '',
        DesignType: ''
      },

      Fill_In_Permissions: 0,

      DealerInfo: {
        Dealer_BaseInfo: {}
      }
    };
  },

  onLoad(options) {
    if (options.DealerId) {
      this.form.DealerId = options.DealerId;
    }
    if (options.Id) {
      this.form.ApplyId = options.Id;
    }

    if (options.SelectType) {
      this.form.ApplyObjectType = options.SelectType;

      if (options.SelectType == 2) {
        this.title = '专卖店设计审核';
      }
      if (options.SelectType == 3) {
        this.title = '其他设计审核';
      }
      if (options.SelectType == 4) {
        this.title = '其他设计审核';
      }
    }
    this.getData();

    this.$setTitle(this.title);
  },

  methods: {
    submit() {
      const designForm = this.$refs.designForm;
      const applyForm = this.$refs.applyForm;
      const reviewForm = this.$refs.reviewForm;

      this.form = Object.assign(this.form, designForm.form);
      this.form.ApplicationDate = applyForm.form.ApplyTime;
      this.form.ApplicationRemake = applyForm.form.Remark;

      this.form.State = reviewForm.form.State;
      this.form.AuditRemark = reviewForm.form.AuditRemark;
      this.form.IsReturnAudit = reviewForm.form.IsReturnAudit;
      this.form.DesignType = designForm.form.DesignType;

      if (!this.form.IsReturnAudit) {
        if (!this.form.State) {
          this.$u.toast('请选择审核状态');
          return false;
        }
      }

      this.$u.api.approvalStoreDesign(this.form).then(res => {
        const { ResultCode, ResultMessage } = res;
        if (ResultCode === 1) {
          this.$refs.uToast.show({
            title: ResultMessage,
            back: true
          });
        }
      });
    },

    getData() {
      this.$u.api
        .getDesignReviewDetail({
          ApplyId: this.form.ApplyId
        })
        .then(res => {
          const { ResultCode, ResultMessage, ResultData, Fill_In_Permissions } = res;

          if (ResultCode === 1) {
            if (this.vuex_user.ManagerId == res.NextManagerId) {
              this.showReview = true;
            }

            this.state = ResultData;
            this.Fill_In_Permissions = Fill_In_Permissions;

            if (this.state.DealerAuditInfo.ObjectType == 4) {
              this.SelectType = 4;
              this.form.ApplyObjectType = 4;
              this.title = '其他设计审核';
              this.tabList = [{ name: '设计信息' }];
            }

            if (this.state.DealerAuditInfo.ObjectType == 5) {
              this.SelectType = 5;
              this.form.ApplyObjectType = 5;
              this.title = '门店平面设计审核';
            }

            if (this.state.ShowLevel == 1) {
              this.DealerInfo.Dealer_BaseInfo = this.state.First_Dealer_BaseInfo;
            }

            if (this.state.ShowLevel == 2) {
              this.DealerInfo.Dealer_BaseInfo = this.state.Double_Dealer_BaseInfo;
            }

            let DesignTypeList = [];
            if (this.form.ApplyObjectType == 5) {
              DesignTypeList = this.state.GraphicDesigns;
            }
            if (this.form.ApplyObjectType == 4) {
              DesignTypeList = this.state.HeadquartersDesigns;
            }
            if (this.form.ApplyObjectType == 3) {
              DesignTypeList = this.state.OtherDesigns;
            }

            this.$refs.designForm.initForm({
              InitialPhotoList: this.state.InitialPhotoList,
              DesignPhotoList: this.state.DesignPhotoList,
              Remark: this.state.DealerAuditInfo.Remark,
              ApplyObjectType: this.state.DealerAuditInfo.ObjectType,
              DoorPlane: this.state.DealerAuditInfo.DoorPlane,
              // 户外广告
              OutDoorAdvertising: this.state.DealerAuditInfo.OutDoorAdvertising,
              // 1+1 媒体
              Media: this.state.DealerAuditInfo.Media,
              DesignType: this.state.DealerAuditInfo.DesignType,
              DesignTypeList
            });

            this.$refs.applyForm.initForm({
              ApplyTime: this.state.DealerAuditInfo.ApplicationDate,
              Remark: this.state.DealerAuditInfo.ApplicationRemake
            });

            this.loadCity = true;
          }
        });
    }
  }
};
</script>

<style lang="scss"></style>
